package org.apache.oozie.tools.workflowgenerator.client.widget.control;

import org.apache.oozie.tools.workflowgenerator.client.OozieWorkflowGenerator;
import org.apache.oozie.tools.workflowgenerator.client.property.control.EndPropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class EndNodeWidget extends NodeWidget {

    public EndNodeWidget(OozieWorkflowGenerator gen) {
        super(gen);
        this.setStyleName("oozie-EndNodeWidget");
        initPropertyTable();
    }

    /**
     * Initialize property table
     */
    protected void initPropertyTable() {

        table = new EndPropertyTable(this);
        generator.setPropertyTable(table);

    }

    /**
     * Return a name of node widget
     */
    @Override
    public String getName() {
        return table.getName();
    }

    /**
     * Set a name of node widget
     *
     * @param s name
     */
    public void setName(String s) {
        table.setName(s);
    }

    /**
     * Generate xml elements of end node and attach them to xml doc
     */
    @Override
    public void generateXML(Document doc, Element root, NodeWidget next) {
        Element endele = doc.createElement("end");
        endele.setAttribute("name", getName());
        root.appendChild(endele);
    }

    @Override
    protected void initEvent() {

        // Register MouseDown Event to update property table of the selected
        // action
        this.addMouseDownHandler(new MouseDownHandler() {

            @Override
            public void onMouseDown(MouseDownEvent event) {
                EndNodeWidget.this.generator.setPropertyTable(EndNodeWidget.this.table);
            }
        });
    }

}
